# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Andrew Smeltzov <andrew.smeltzov@gmail.com>

_realname=shiboken2
pkgbase=mingw-w64-${_realname}-qt5
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname}-qt5)
pkgdesc="CPython bindings generator for C++ libraries (mingw-w64)"
pkgver=5.15.2
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://doc.qt.io/qtforpython-5/"
license=("LGPL")
depends=("${MINGW_PACKAGE_PREFIX}-clang"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-qt5")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-libxslt")
options=('staticlibs' 'strip')
install=${_realname}-${CARCH}.install
source=(https://download.qt.io/official_releases/QtForPython/pyside2/PySide2-${pkgver}-src/pyside-setup-opensource-src-${pkgver}.tar.xz
        001-shiboken.patch
        002-cmake-relative-paths.patch
        003-llvm-paths.patch
        004-shibokenmodule-name.patch)
sha256sums=('b306504b0b8037079a8eab772ee774b9e877a2d84bab2dbefbe4fa6f83941418'
            '1dd8273f99c2d1a9e82f7c0c3c03afcc73ce9bc75e68b8bd1863cf41674da25d'
            'e8fce91fee692a3ef906c841575e53e141a58c0c97c63fd1d94bed5a823190f9'
            'b7af4880b6b786d5e635d0b2cf45b41ba977581356b7b8b5ead81effac5eb9ec'
            'f163bc1f81b3982b8445f06e7de4e0398c84a1935c5be7b6dc0e88f4ed794d5f')

prepare() {
  cd ${srcdir}/pyside-setup-opensource-src-${pkgver}
  patch -p1 -i ${srcdir}/001-shiboken.patch
  patch -p1 -i ${srcdir}/002-cmake-relative-paths.patch
  patch -p1 -i ${srcdir}/003-llvm-paths.patch
  patch -p1 -i ${srcdir}/004-shibokenmodule-name.patch
}

build() {
  CXXFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_C_COMPILER=${MINGW_PREFIX}/bin/clang.exe \
    -DCMAKE_CXX_COMPILER=${MINGW_PREFIX}/bin/clang++.exe \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_TESTS=OFF \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    ../pyside-setup-opensource-src-${pkgver}/sources/shiboken2

  make

}

fix_paths() {
  local prefixwin=$(cygpath -am ${MINGW_PREFIX})
  local prefixinst=$(cygpath -am ${pkgdir}${MINGW_PREFIX})

  local _files=$(find ${pkgdir}${MINGW_PREFIX}/lib -type f -iname "*.cmake" -o -iname "*.pc")
  for f in ${_files}; do
    plain "Patching ${f}..."
    sed -e "s|${prefixwin}|${MINGW_PREFIX}|g" \
        -e "s|${prefixinst}|${MINGW_PREFIX}|g" \
        -i ${f}
  done
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  fix_paths
}
